/**
 * 个人空间相关功能JS文件
 * 
 * @author		布衣才子
 * @date		2012-09-27
 * @email		work.jerryliu@gmail.com
 * @qq			394969553
 * @version		v1.0
 * @copyright	copyright 2012-2014	YeeYi.com All Rights Reserved	
 */

/**
 * 
 * 服务器访问地址
 */
var apiHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";
var b64 = new Base64();

/**
 * 网络访问函数
 * @param {Object} url	请求的网络地址
 * @param {Object} callback	回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 获取用户基本信息 
 */
function getUserInfo(){
	var uid = getLocalStorge('uid');
	if(!uid){
		uexWindow.alert('提示','您还未登录或登录信息已过期！','确定');
		return false;
	}
	var RequestURL = apiHost + '&ac=user&uid='+uid;
	xmlHttp(RequestURL,showInfo);
}


/**
 * 处理服务器返回的用户信息
 * @param {Object} data 用户信息
 */
function showInfo(data){
	var returnCode = data.code;
	if(returnCode == '404'){
		uexWindow.toast("0","5","请求失败，未找到用户信息…","2000");
	}else if(returnCode == '200'){
		var username = b64.decode(data.username);
		var uid = getLocalStorge('uid');
		$('#user').text(username);
		$('#regDate').text(data.regdate);
		$('#group').text(b64.decode(data.groupname));
		$('#credits').text(data.credits);
		$('#money').text(data.money);
		$('#flower').text(data.flower);
		var newpm = parseInt(data.newpm);
		var newprompt = parseInt(data.newprompt);
		var groupid = getLocalStorge('groupid');
		if (in_array(groupid, ['1', '2', '3', '57', '62', '42'])) {
			$('.isShow').addClass('ub');
		}
		
		if(newpm > 0){
			$('#newpm').html('<font color="red">'+newpm+'</font>');
		}
		if(newprompt > 0){
			$('#newprompt').html('<font color="red">'+newprompt+'</font>');
		}
		$('#face').attr('src','http://center.yeeyi.com/avatar.php?uid='+uid+'&size=small');
	}else{
		uexWindow.alert('提示','信息获取失败！','确定');
	}
}

/**
 * 打开提醒窗口
 */
function openNoticeWindow(){
	var nums = $('#newprompt').text();
	$('#newprompt').text('0');
	if(nums != '0'){
		changeStatus('newprompt');
	}
	uexWindow.open('notice','0','notice.html','2','100%','100%',4);
}

/**
 * 打开短消息窗口
 */
function openMessageWindow(){
	var nums = $('#newpm').text();
	$('#newpm').text('0');
	if(nums != '0'){
		changeStatus('newpm');
	}
	uexWindow.open('message','0','message_list.html','2','100%','100%',4);
}

/**
 * 改变在线提醒或短信息状态
 * @param {Object} type
 */
function changeStatus(type){
	localStorage.setItem('noticeType',type);
	var uid = getLocalStorge('uid');
	var pwd = getLocalStorge('pwd');
	var url = apiHost + '&ac=message&type=change&uid='+uid+'&pwd='+pwd+'&do='+type; 
	xmlHttp(url,showChange);
}

function showChange(data){
	//Do nothing
}

/**
 * 打开自己帖子列表窗口
 */
function openMyThreadWindow(){
	uexWindow.open('mythread','0','mythread.html','2','100%','100%',4);
}

/**
 * 打开“关于亿忆网”窗口
 */
function openAboutWindow(){
	uexWindow.open('about','0','about.html','2','100%','100%',0);
}

/**
 * 清楚缓存
 */
function clearCache(){
	localStorage.setItem('chType','clear');
	var confirmButtom = new Array();
	confirmButtom[0] = '清 除';
	confirmButtom[1] = '返 回';
	uexWindow.confirm('提示','该操作将清除所有缓存图片和登录信息！',confirmButtom);
}

/**
 * confirm控件监控函数
 * @param {Object} opId
 * @param {Object} dataType
 * @param {Object} data
 */
function confirmSuccess(opId,dataType,data){
	var chType = getLocalStorge('chType');
	if(data == 0 && chType == 'clear'){
		zy_clearcache();
		uexWindow.evaluateScript("root","0","defaultWindow()");
	}
	
	if(data == 0 && chType == 'update'){
		var platform = uexWidgetOne.getPlatform();
		if (data == 0){
			if (platform == 0){
				var downurl = getLocalStorge('downUrl');
				if(!downurl){
					uexWindow.alert("","未找到下载链接，请重试！","返 回");
				}else{
					//uexWidget.loadApp(downurl);
					uexWindow.alert("","目前有最新版本，请到appstore下载更新！","返 回");
				}
			}else if(platform == 1){
				uexDownloaderMgr.createDownloader('2012');
			}
		}
	}
	
	if(data == 0 && chType == 'install'){
		var myDate=new Date();
		var str = myDate.getFullYear().toString() + myDate.getMonth().toString() + myDate.getDate().toString();
		uexWidget.installApp('wgt://upgrade/yeeyi_'+str+'.apk');
	}
}

function cbDownloader(opId, dataType, data){
	if(data == 0){
		var downurl = getLocalStorge('downUrl');
		if(!downurl){
			uexWindow.alert("","未找到下载链接，请重试！","返 回");
		}else{
			var myDate=new Date();
			var str = myDate.getFullYear().toString() + myDate.getMonth().toString() + myDate.getDate().toString();
			uexDownloaderMgr.download('2012', downurl, 'wgt://upgrade/yeeyi_'+str+'.apk', 0);
		}
		
	}
}

function downLoaderStatus(opId, fileSize, percent, status){
	if (status == 0){
		$('#updateTips').html('下载已完成' + percent + '%');
	}else if (status == 1){
		$('#updateTips').html('下载已完成');
		uexDownloaderMgr.closeDownloader('2012');
		localStorage.setItem('chType','install');
		uexWindow.confirm('', '下载完成，是否安装?', ['立即安装', '稍后安装']);
	}else{
		alert('下载出错！');
	}
}

/**
 * 退出当前账号
 */
function logout(){
	uexWindow.toast("1","5","正在退出，页面将跳转到论坛首页…","3000");
	localStorage.removeItem('uid');
	localStorage.removeItem('username');
	localStorage.removeItem('pwd');
	localStorage.removeItem('groupid');
	setTimeout(function () {
			//zy_clearcache();
	}, 3000);
	uexWindow.evaluateScript("root","0","defaultWindow()");
}

/**
 * 检查软件更新
 */
function checkUpdate(){
	uexWindow.toast("1","5","软件版本检测中…","0");
	var ver = getLocalStorge('ver');
	var platform = getLocalStorge('platform');
	if(ver && platform){
		var url = apiHost + "&ac=version&ver="+ver+"&platform="+platform;
		xmlHttp(url,cbcheckUpdate);
	}else{
		uexWindow.alert('提示','检测失败，请重试！','返回');
	}
}

function getWidgetInfo(opId,dataType,data){
		var obj = JSON.parse(data);
		var ver = obj.version;
		localStorage.setItem('ver',ver);
}

function cbcheckUpdate(data){
	uexWindow.closeToast();
	localStorage.setItem('chType','update');
	if (data.result == '0')
		{
			var platform = getLocalStorge('platform');
			if(platform == 0){
				uexWindow.alert("","目前有最新版本，请到appstore下载更新！","返 回");
			}else{
				localStorage.setItem("downUrl",data.url);
				var confirmButtom = new Array();
				confirmButtom[0] = '现在更新';
				confirmButtom[1] = '暂不更新';
				uexWindow.confirm('升级提示', '当前有新版本，是否更新?', confirmButtom);
			}
			
		}else{
			uexWindow.alert("","版本已是最新！","返 回");
		}
}

/**
 * HTML5 localStorge.getItem 函数的封装
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}

function in_array (needle, haystack, argStrict) {
    var key = '',strict = !! argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
